﻿using Abp.Configuration.Startup;
using Microsoft.AspNetCore.Http;
using Mt.Site.Core.Base.Web;
using Mt.Site.Core.Url;

namespace Mt.Site.Core
{
    public class MtGlobalizationHelper : IGlobalizationHelper
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IMultiTenancyConfig _multiTenancyConfig;

        public MtGlobalizationHelper(
            IHttpContextAccessor httpContextAccessor,
            IMultiTenancyConfig multiTenancyConfig)
        {
            _httpContextAccessor = httpContextAccessor;
            _multiTenancyConfig = multiTenancyConfig;
        }

        public string CurrentTenantIdCookieName
        {
            get
            {
                if (IsFrontEnd)
                {
                    return MtSiteConsts.CookieNames.FrontEndTenantId;
                }
                else
                {
                    return MtSiteConsts.CookieNames.backendTenantId;
                }
            }
        }

        public string CurrentCultureNameCookieName
        {
            get
            {
                if (IsFrontEnd)
                {
                    return MtSiteConsts.CookieNames.FrontEndCultureName;
                }
                else
                {
                    return MtSiteConsts.CookieNames.BackEndCultureName;
                }
            }
        }

        public string FrontEndCultureNameCookieName => MtSiteConsts.CookieNames.FrontEndCultureName;

        public string FrontEndTenantIdCookieName => MtSiteConsts.CookieNames.FrontEndTenantId;

        #region IsFrontEnd

        private const string IsFrontEndCacheItemKey = "Mt.IsFrontEndCacheItem";
        
        public virtual bool IsFrontEnd
        {
            get
            {
                try
                {
                    if (_httpContextAccessor.HttpContext == null)
                    {
                        return false;
                    }

                    bool? item = _httpContextAccessor.HttpContext.Items[IsFrontEndCacheItemKey] as bool?;
                    if (item.HasValue)
                    {
                        return item.Value;
                    }
                    return CheckIsFrontEnd(UrlChecker.GetAbsolutePath(_httpContextAccessor.HttpContext.Request));
                }
                catch
                {
                    return false;
                }
            }
        }

        public bool IsUserOnlyBelongToBackend => false;

        public bool CheckIsFrontEnd(string regularUrl, bool isLower = false)
        {
            bool result = IsFrontEndUrl(regularUrl, isLower);

            var httpContext = _httpContextAccessor.HttpContext;
            if (httpContext != null)
            {
                httpContext.Items[IsFrontEndCacheItemKey] = result;
            }

            return result;
        }

        public bool IsFrontEndUrl(string regularUrl, bool isLower = false)
        {
            //return false;

            if (regularUrl == null)
            {
                return false;
            }

            string regularUrlToLower = isLower ? regularUrl : regularUrl.ToLower();

            if (regularUrlToLower.StartsWith(Mt.Site.MtSiteConsts.BackgroundBaseUrl.Url_Lower + "/") 
                || regularUrlToLower == Mt.Site.MtSiteConsts.BackgroundBaseUrl.Url_Lower)
            {
                return false;
            }

            if (regularUrlToLower.StartsWith("/api/services/app/"))
            {
                return false;
            }

            if (regularUrlToLower.StartsWith("/api/tokenauth/"))
            {
                return false;
            }

            if (regularUrlToLower.StartsWith("/abpscripts/")  //AbpScripts/GetScripts
                || regularUrlToLower.StartsWith("/abpuserconfiguration/") //AbpUserConfiguration/GetAll
                || regularUrlToLower.StartsWith("/abplocalization/") //AbpLocalization/ChangeCulture
                || regularUrlToLower.StartsWith("/signalr")
                || regularUrlToLower.StartsWith("/abpserviceproxies/getall")//AbpServiceProxies/GetAll
                || regularUrlToLower.StartsWith("/abpappview/")   //AbpAppView/Load
                    )
            {
                return false;
            }

            if (regularUrlToLower.StartsWith("/hangfire"))
            {
                return false;
            }

            return true;
        }

        #endregion
    }
}
